package db

import (
	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

func New(dns string) (db *gorm.DB, err error) {
	db, err = gorm.Open(mysql.Open(dns), &gorm.Config{})
	if err != nil {
		return db, err
	}
	err = autoMigrate(db)
	if err != nil {
		return db, err
	}
	return db, err
}

func autoMigrate(db *gorm.DB) error {
	dst := []interface{}{
		&Configration{},
		&Server{},
		&ProjectTask{},
		&Project{},
		&ProjectBuild{},
		&ProjectDeploy{},
	}

	return db.AutoMigrate(dst...)
}
